﻿using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace LogParser
{
    public static class BdiFileParser
    {
        public static string GetLogFileName()
        {
            DirectoryInfo folder = new DirectoryInfo("LogFile");
            if (folder.Exists)
            {
                foreach (FileInfo file in folder.GetFiles())
                {
                    return file.FullName;
                }
            }

            return string.Empty;
        }

        public static ArrayList ReadFile(string fileName)
        {
            ArrayList list = new ArrayList();

            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);

                    string str = sr.ReadLine();
                    while (str != null)
                    {
                        list.Add(str);
                        str = sr.ReadLine();
                    }

                    sr.Close();
                }
                fs.Close();
            }
            return list;
        }

        public static string GetRequestHeader(string str)
        {
            return GetMidValue(str, "BDIGenericRequestHeaders={", "}");
        }

        public static string GetRequestBody(string str)
        {
            return GetMidValue(str, "BDIGenericRequestBody={Body=", "}");
        }

        private static string GetMidValue(string str, string begin, string end)
        {
            Regex reg = new Regex("(?<=(" + begin + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            return reg.Match(str).Value;  
        }
    }
}
